package com.gewu.pm.ui.activity.promotion;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.PromotionBean;
import com.gewu.pm.ui.activity.approval.ApprovalApplicationActivity;
import com.gewu.pm.ui.adapter.terminal.TerminalPromotionAdapter;
import com.gewu.pm.ui.activity.terminal.EventDetailsActivity;
import com.gewu.pm.ui.dialog.DateDialog;
import com.gewu.pm.utils.eventbus.EventBean;
import com.gewu.pm.utils.eventbus.EventCode;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 促销管理页面
 */
public final class PromotionManagementActivity extends AppActivity
        implements BaseAdapter.OnItemClickListener {

    private TextView mDateRangeView;
    private TextView mRecordCountView;
    private TextView mMyApplicationView;
    private RecyclerView mRecyclerView;
    private TextView mCreateButton;

    private TerminalPromotionAdapter mAdapter;
    private List<PromotionBean> mDataList = new ArrayList<>();

    public static void start(Context context) {
        Intent intent = new Intent(context, PromotionManagementActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.promotion_management_activity;
    }

    @Override
    protected void initView(android.os.Bundle savedInstanceState) {
        mDateRangeView = findViewById(R.id.tv_date_range);
        mRecordCountView = findViewById(R.id.tv_record_count);
        mMyApplicationView = findViewById(R.id.tv_my_application);
        mRecyclerView = findViewById(R.id.rv_promotion_list);
        mCreateButton = findViewById(R.id.tv_bottom_button);

        // 设置 RecyclerView
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TerminalPromotionAdapter(this);
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

        setOnClickListener(mDateRangeView, mMyApplicationView, mCreateButton);
    }

    @Override
    protected void initData() {
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        mDataList.clear();
        mDataList.add(new PromotionBean("买赠活动", 1));
        mDataList.add(new PromotionBean("买赠活动", 1));
        mDataList.add(new PromotionBean("买赠活动", 1));
        mDataList.add(new PromotionBean("满减活动", 2));

        mAdapter.setData(mDataList);
        updateRecordCount();
    }

    /**
     * 更新记录数
     */
    private void updateRecordCount() {
        mRecordCountView.setText("共" + mDataList.size() + "条记录");
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.tv_date_range) {// 选择日期范围
            new DateDialog.Builder(this)
                    .setTitle(getString(R.string.date_title))
                    // 确定按钮文本
                    .setConfirm(getString(R.string.common_confirm))
                    // 设置 null 表示不显示取消按钮
                    .setCancel(getString(R.string.common_cancel))
                    .setListener(new DateDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, int year, int month, int day) {
                            String time = year + getString(R.string.common_year) + month + getString(R.string.common_month) + day + getString(R.string.common_day);
                            toast(time);
                            mDateRangeView.setText(time);
                        }
                    })
                    .show();
        } else if (id == R.id.tv_my_application) {// 我的申请与审批
            // TODO: 打开我的申请与审批
            ApprovalApplicationActivity.start(getActivity());

        } else if (id == R.id.tv_bottom_button) {
            // 创建买赠活动
            PromotionCreateActivity.start(this);
        }
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        PromotionBean bean = mAdapter.getItem(position);
        // 跳转到活动详情
        EventDetailsActivity.start(this, bean.getType());
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(EventBean event) {
        if (event.getCode() == EventCode.PROMOTION_CREATED) {
            // 接收创建完成事件，刷新列表
            loadData();
        }
    }
}

